package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.TradeList;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.clientpackets.L2GameClientPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.network.serverpackets.TradeStats;

/**
 *  客户端提交锁定交易协议
 *  锁定的时候 会 下交易放下发物品列表
 * @author zjj
 *
 */
public final class TradeLock extends L2GameClientPacket {

	private static final Logger _log = LoggerFactory.getLogger(L2GameClientPacket.class.getName());

	private static final String _S__76_TRADELOCK = "[S] 0x76 TradeLock";

	private int _flag; // 0锁定， 1解锁 

	@Override
	public String getType() {
		return _S__76_TRADELOCK;
	}

	@Override
	protected void readImpl() {
		_flag = readByte();
	}

	@Override
	protected void runImpl() {

		//请求锁定的角色不存在
		final FuzePcInstance player = getClient().getActiveChar();
		if (player == null)
			return;

		final TradeList trade = player.getActiveTradeList();
		if( trade == null ){
			return;
		}
		final FuzePcInstance partner = trade.getPartner();
		if( partner == null ){
			return;
		}
		final TradeList partnerTradelist = partner.getActiveTradeList();

		//解锁
		if (_flag == TradeStats.STAT_UNLOCK) {
			//交易链不存在

			if (trade == null) {
				_log.warn("Character: " + player.getName() + "UnLock Trade without active tradelist", Config.SERVER_ID);
				return;
			}

			if (partner == null) {
				_log.warn("Character: " + player.getName() + "UnLock Trade without active player", Config.SERVER_ID);
				return;
			}

			if (partnerTradelist == null) {
				_log.warn("Character: " + player.getName() + "UnLock Trade without active tradelist", Config.SERVER_ID);
				return;
			}

			//任何一放解锁， 双方都会解锁
			trade.unLock();
			trade.invalidateConfirmation();
			partnerTradelist.unLock();
			partnerTradelist.invalidateConfirmation();

			//通知交易双方 解锁锁定
			trade.getOwner().sendPacket(new TradeStats(TradeStats.STAT_UNLOCK, TradeStats.STAT_SELF));
			partnerTradelist.getOwner().sendPacket(new TradeStats(TradeStats.STAT_UNLOCK, TradeStats.STAT_PARTEN));

			return;

		}// end if( _flag == 1 )

		//交易链不存在
		if (trade == null) {
			_log.warn("Character: " + player.getName() + "Lock Trade without active tradelist", Config.SERVER_ID);
			return;
		}

		if (partner == null || player.getKnownList().getKnownCharacters().get(partner.getObjectId()) == null || partner.getActiveTradeList() == null) {
			// 交易的对方不存在
			if (partner != null)
				_log.warn("Character:" + player.getName() + " requested invalid trade", Config.SERVER_ID);

			SystemMessage msg = new SystemMessage(SystemMessageId.TARGET_IS_NOT_FOUND_IN_THE_GAME);
			player.sendPacket(msg);
			player.cancelActiveTrade();
			return;
		}

		if (!player.getAccessLevel().allowTransaction()) {
			player.sendMessage("Transactions are disable for your Access Level");
			player.cancelActiveTrade();
			return;
		}

		trade.lock();

		//通知交易双方 锁定交易列表
		trade.getOwner().sendPacket(new TradeStats(TradeStats.STAT_LOCK, TradeStats.STAT_SELF));
		partnerTradelist.getOwner().sendPacket(new TradeStats(TradeStats.STAT_LOCK, TradeStats.STAT_PARTEN));

		//TODO:
		//现改为 每增加一个物品 就通知交易的另一方更新， 所有在物品锁定的时候 将不在需要向双方发送交易列表
		//		trade.addItem( _itemInfo );
		//		//如果交易链表中 存在有效的物品 那么通知交易双方
		//		if( trade.getItemCount() > 0 )
		//		{
		//			player.sendPacket(new TradeLockItems( trade.getItems(), TradeLockItems.ITEM_LIST_SELF ));
		//			trade.getPartner().sendPacket(new TradeLockItems( trade.getItems() , TradeLockItems.ITEM_LIST_OTHER ));
		//		}

	}

}
